﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using FCS.Communication;

namespace FCS.Pattern
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static void Main()
		{
			AppDomain currentDomain = AppDomain.CurrentDomain;
			currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

			ServiceBase[] ServicesToRun;
			ServicesToRun = new ServiceBase[] 
			{ 
				new MyDefaultService() 
			};

            while (true)
            {
                try
                {
                    ServiceBase.Run(ServicesToRun);
                }
                catch (Exception e)
                {
                    EventLogWriter _logger = new EventLogWriter("Default Sequence Service");
                    _logger.WriteToLog(System.Diagnostics.EventLogEntryType.Error, "Unhandled error & restarting service: " + e.Message + "\n" + e.StackTrace);
                }
            }
		}

		static void MyHandler(object sender, UnhandledExceptionEventArgs args)
		{
            EventLogWriter _logger = new EventLogWriter("Default Sequence Service");
			Exception e = (Exception)args.ExceptionObject;
            _logger.WriteToLog(System.Diagnostics.EventLogEntryType.Error, "Unhandled error: " + e.Message + "\n" + e.StackTrace);
		}
	}
}
